package com.toddding.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.toddding.domain.File;
import com.toddding.domain.Folder;
import com.toddding.domain.User;
import com.toddding.mapper.FileMapper;
import com.toddding.mapper.FolderMapper;
import com.toddding.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class FileServiceImpl implements FileService {
    @Autowired
    private FileMapper fileMapper;

    @Autowired
    private FolderMapper folderMapper;

    @Autowired
    private HttpSession session;

    /**
     * 分页查询文件
     *
     * @param keyWord
     * @param page
     * @return
     */
    @Override
    public PageInfo<File> queryPage(String keyWord, Integer page) {
        Page<File> filePage = PageHelper.startPage(page, 5);
        this.fileMapper.selectFiles(keyWord);
        return filePage.toPageInfo();
    }

    /**
     * 上传文件
     *
     * @param files
     * @param folderId
     */
    @Override
    public void save(MultipartFile[] files, Integer folderId) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getUserId();
        String realPath = session.getServletContext().getRealPath("file");
        //1. 将文件保存在物理磁盘
        //2. 将相关数据保存数据库
        // 待保存到数据库中文件数据
        List<File> fileList = new ArrayList<>();
        for (MultipartFile file : files) {
            String oldName = file.getOriginalFilename();
            // 乘以100 放大100倍 强转int 丢失小数位
            int size = (int) (file.getSize() / 1024.0 / 1024.0 * 100);
            // 除以100还原 保留了2位小数
            String fileSize = (size / 100.0) + "M";
            String type = FileUtil.getSuffix(oldName);
            String pathName = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS").toString();
            String newName = pathName + "." + type;
            // 文件预保存的物理路径
            Folder folder = this.folderMapper.selectById(folderId);
            String filePath = realPath + java.io.File.separator + folder.getFolderName() + java.io.File.separator + newName;
            File saveFile = new File();
            saveFile.setOldName(oldName);
            saveFile.setFileSize(fileSize);
            saveFile.setFileType(type);
            saveFile.setFilePath(filePath);
            saveFile.setFolderId(folderId);
            saveFile.setUserId(userId);
            // 将对象放入容器
            fileList.add(saveFile);
            // 将文件保存在物理磁盘
            try {
                file.transferTo(new java.io.File(filePath));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 批量新增文件
        this.fileMapper.batchInsert(fileList);
    }

    /**
     * 根据ID查询文件信息
     *
     * @param fileId
     * @return
     */
    @Override
    public File queryFile(Integer fileId) {
        return this.fileMapper.selectById(fileId);
    }

    /**
     * 根据 ID 删除文件
     *
     * @param fileId
     */
    @Override
    public void delete(Integer fileId) {
        // 删除文件
        File myFile = this.fileMapper.selectById(fileId);
        java.io.File file = new java.io.File(myFile.getFilePath());
        if (file.exists()) {
            file.delete(); // 删除
        }
        //删除数据
        this.fileMapper.deleteById(fileId);

    }
}
